package easyjs

import (
	"os"
	"path/filepath"
	"strings"

	"gitee.com/haodreams/golib/logs"
	"gitee.com/haodreams/libs/easy"
)

type FS struct {
}

func (m *FS) Base(path string) string {
	return filepath.Base(path)
}

// 读取全部行，出错显示msg+错误提示
func (m *FS) ReadLines(path, msg string) []string {
	lines, err := easy.ReadLines(path)
	if err != nil {
		logs.LiteWarn(msg, err)
	}
	return lines
}

// 读取全部行，出错显示msg+错误提示,返回map[键]=值 
func (m *FS) CsvToMap(path, msg string) map[string]string {
	lines, err := easy.ReadLines(path)
	if err != nil {
		logs.LiteWarn(msg, err)
	}
	
	linesMap := make(map[string]string)
	for _, line := range lines {
		parts := strings.SplitN(line, ",", 2)
		if len(parts) != 2 {
			continue
		}
		linesMap[parts[0]] = parts[1]
	}
	return linesMap
}

// 读取文件
func (m *FS) ReadFile(path, msg string) any {
	data, err := os.ReadFile(path)
	if err != nil {
		logs.LiteWarn(msg, err)
		return nil
	}
	return string(data)
}

/**
 * @description: 删除文件
 * @param {string} path
 * @return {*}
 */
func (m *FS) Remove(path string) any {
	_, err := os.Stat(path)
	if err != nil {
		return nil
	}
	err = os.Remove(path)
	if err != nil {
		return err
	}
	return nil
}

/**
 * @description: 文件或目录是否存在
 * @param {string} path
 * @return {*}
 */
func (m *FS) Exist(path string) bool {
	_, err := os.Stat(path)
	return err == nil
}

// 读取文件目录
func (m *FS) ReadDir(path string) (paths []string) {
	ps, err := os.ReadDir(path)
	if err != nil {
		logs.Warn(err)
		return nil
	}
	for _, p := range ps {
		paths = append(paths, p.Name())
	}
	return
}

/**
 * @description: 是否是一个文件
 * @param {string} path
 * @return {*}
 */
func (m *FS) IsFile(path string) bool {
	f, err := os.Stat(path)
	if err != nil {
		return false
	}
	if f.IsDir() {
		return false
	}
	return true
}

/**
 * @description: 新建一个目录
 * @param {string} path
 * @return {*}
 */
func (m *FS) Mkdir(path string) bool {
	err := easy.MkdirAll(path)
	return err == nil
}

/**
 * @description: 文件改名
 * @param {*} old
 * @param {string} new
 * @return {*}
 */
func (m *FS) Rename(old, new string) any {
	_, err := os.Stat(old)
	if err == nil {
		err = os.Rename(old, new)
		if err != nil {
			return err.Error()
		}
		return nil
	}
	return err.Error()
}

/**
 * @description: 获取文件的更新时间
 * @param {string} path
 * @return {*}
 */
func (m *FS) FileModiTime(path string) int64 {
	fi, err := os.Stat(path)
	if err != nil {
		return -1
	}
	return fi.ModTime().Unix()
}

/**
 * @description: 是否是一个目录
 * @param {string} path
 * @return {*}
 */
func (m *FS) IsDir(path string) bool {
	f, err := os.Stat(path)
	if err != nil {
		return false
	}
	if f.IsDir() {
		return true
	}
	return false
}

/**
 * @description: 追加的方式文件
 * @param {*} path
 * @param {string} data
 * @return {*}
 */
func (m *FS) AppendFile(path, data string) any {
	f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = f.WriteString(data)
	return err
}

/**
 * @description: 写文件
 * @param {*} path
 * @param {string} data
 * @return {*}
 */
func (m *FS) WriteFile(path, data string) any {
	dir := filepath.Dir(path)
	os.MkdirAll(dir, 0755)
	return os.WriteFile(path, []byte(data), 0644)
}

// 增加打开文件操作
func (m *FS) OpenFile(path, mode string) *os.File {
	flag := os.O_RDWR
	if strings.HasPrefix(mode, "w") {
		flag |= os.O_TRUNC | os.O_CREATE
	}
	if strings.Contains(mode, "+") {
		flag |= os.O_CREATE
	}
	if strings.Contains(mode, "a") {
		flag |= os.O_APPEND
	}
	f, err := os.OpenFile(path, flag, 0644)
	if err != nil {
		return nil
	}
	return f
}
